package com.changgou.pay.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.pay.service.WeiXinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WeiXinPayController
 * @Description
 * @Author Mr.L
 * @Date 18:58 2019/11/26
 * @Version 2.1
 *
 *   aof  rdb
 *
 **/
@RestController
@RequestMapping("/weixin/pay")
@CrossOrigin
public class WeiXinPayController {

    @Autowired
    private WeiXinPayService weiXinPayService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private Environment env;

    /**
     * 生成订单支付二维码
     * @param money
     * @return
     */
    @RequestMapping("/create/native")
    public Result createNative(@RequestParam Map<String, String> money){
//        Map<String, String> map = weiXinPayService.createNative(outtradeno, money);
        Map<String, String> map = weiXinPayService.createNative(money);
        return new Result(true, StatusCode.OK,"创建支付二维码成功",map);
    }

    /**
     * 查询订单支付状态
     * @param out_trade_no
     * @return
     */
    @RequestMapping("/status/query")
    public Result queryStatus(String out_trade_no){
        Map<String,String> map = weiXinPayService.queryPayStatus(out_trade_no);
        return new Result(true,StatusCode.OK,"查询状态成功！", map);
    }

//    @RequestMapping("/notify/url")
//    public String notifyUrl(HttpServletRequest request) throws Exception {
//        // 获取微信通知的数据
//        ServletInputStream is = request.getInputStream();
//        // 将数据读取到内存中
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        byte[] buffer = new byte[1024];     // 定义缓冲区
//        int len = 0;
//        while ((len = is.read(buffer)) != -1){
//            os.write(buffer, 0, len);
//        }
//        os.flush();
//        os.close();
//        is.close();
//
//        // 处理结果（回调的数据）
//        String strXML = new String(os.toByteArray(), "UTF-8");
//        Map<String, String> map = WXPayUtil.xmlToMap(strXML);
//        System.out.println("获取微信的通知结果：" + map);
//
//        // 将通知结果发送到mq中  改方法，Object转成string
////        String exchange = env.getProperty("mq.pay.exchange.order");
////        String routingKey = env.getProperty("mq.pay.routing.key");
////        // 将回调数据发送mq
////        rabbitTemplate.convertAndSend(env.getProperty("mq.pay.exchange.order"),env.getProperty("mq.pay.routing.key"), JSON.toJSONString(map));
//        // 获取附加数据
//        String attach = map.get("attach");
//        Map<String, String> attachMap = JSON.parseObject(attach, Map.class);
//        // 将通知结果发送到mq中
//        String exchange = env.getProperty(attachMap.get("exchange"));
//        String routingKey = env.getProperty(attachMap.get("routingKey"));
//        // 将回调数据发送mq
//        rabbitTemplate.convertAndSend(exchange, routingKey, JSON.toJSONString(map));
//        return "success";
//    }

    @RequestMapping("/notify/url")
    public String notifyUrl(@RequestBody Map<String,String> map) throws Exception {

        System.out.println("获取微信的通知结果：" + map);

        // 将通知结果发送到mq中  改方法，Object转成string
//        String exchange = env.getProperty("mq.pay.exchange.order");
//        String routingKey = env.getProperty("mq.pay.routing.key");
//        // 将回调数据发送mq
//        rabbitTemplate.convertAndSend(env.getProperty("mq.pay.exchange.order"),env.getProperty("mq.pay.routing.key"), JSON.toJSONString(map));
        // 获取附加数据
        String attach = map.get("attach");
        Map<String, String> attachMap = JSON.parseObject(attach, Map.class);
        // 将通知结果发送到mq中
//        String exchange = env.getProperty(attachMap.get("exchange"));
//        String routingKey = env.getProperty(attachMap.get("routingKey"));
        String exchange = "exchange.seckillorder";
        String routingKey = "queue.seckillorder";
        Map<String, String> map2 = new HashMap<>();
        map2.put("usename", "tom");
        // 将回调数据发送mq
        rabbitTemplate.convertAndSend(exchange, routingKey, JSON.toJSONString(map2));
        return "success";
    }


}
